package cn.cncoders.keep.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import cn.coders.net.application.utils.HttpsRequestUtils;
import cn.coders.net.application.utils.logger.LoggerNames;
import cn.coders.net.application.utils.logger.LoggerUtil;

public class KeepLogin {
	/**
	 * 日志操作
	 */
	private static final Logger logger = Logger.getLogger(LoggerNames.WEB_LOGGER);
	/**
	 * 操作名称
	 */
	private static final String OPERATION_NAME = "\n【cookie保持测试】-->";
	static CookieStore cookieStore = null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// CloseableHttpClient client=new HttpClients.createDefault();
		String luhongurl = "http://cms.luhong168.com/admin/main/login";
		// doPost(luhongurl);
		// dolist("http://cms.luhong168.com/Admin/order/qihuo");
		cookieStore = new BasicCookieStore();
		String admin_name = "wangyin";
		String admin_pass = "5sgGY6iccarD9ch";
		String user_word = "admin";
		String code = "198707102171";
		Map parameterMap = new HashMap();

		parameterMap.put("admin_name", admin_name);
		parameterMap.put("admin_pass", admin_pass);
		parameterMap.put("user_word", user_word);
		parameterMap.put("code", code);
		String result = HttpsRequestUtils.keepLoginPost(luhongurl, parameterMap, cookieStore,null);
		LoggerUtil.info(logger, OPERATION_NAME + result);
		Header header=null;
		String liststr = HttpsRequestUtils.keepLoginPost("http://cms.luhong168.com/Admin/order/qihuo", null,
				cookieStore,null);
		LoggerUtil.info(logger, OPERATION_NAME + liststr);
	}

	public static void dolist(String url) {
		CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost post = new HttpPost(url);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		post.setHeader("Referer", "http://cms.luhong168.com/admin/order/qihuo.html");
		post.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
		CloseableHttpResponse response;
		try {
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				System.out.println(cookieStore);
				LoggerUtil.info(logger, OPERATION_NAME + JSON.toJSONString(cookieStore));

				LoggerUtil.info(logger, OPERATION_NAME + JSON.toJSONString(cookieStore.getCookies()));
				HttpEntity entity = response.getEntity();
				LoggerUtil.info(logger, OPERATION_NAME + EntityUtils.toString(entity));

			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String doPost(String url) {

		String admin_name = "wangyin";
		String admin_pass = "5sgGY6iccarD9ch";
		String user_word = "admin";
		String code = "198707102171";
		cookieStore = new BasicCookieStore();
		CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost post = new HttpPost(url);
		post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		post.setHeader("Referer", "http://cms.luhong168.com/admin/order/qihuo.html");
		post.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");

		Map parameterMap = new HashMap();
		parameterMap.put("admin_name", admin_name);
		parameterMap.put("admin_pass", admin_pass);
		parameterMap.put("user_word", user_word);
		parameterMap.put("code", code);
		try {
			UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(getParam(parameterMap), "UTF-8");
			post.setEntity(postEntity);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		try {
			CloseableHttpResponse response = client.execute(post);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				System.out.println(cookieStore);
				LoggerUtil.info(logger, OPERATION_NAME + JSON.toJSONString(cookieStore));

				LoggerUtil.info(logger, OPERATION_NAME + JSON.toJSONString(cookieStore.getCookies()));
				HttpEntity entity = response.getEntity();
				LoggerUtil.info(logger, OPERATION_NAME + EntityUtils.toString(entity));

			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
		// return response.toString();
		return "";
	}

	public static List<BasicNameValuePair> getParam(Map parameterMap) {
		List<BasicNameValuePair> param = new ArrayList<BasicNameValuePair>();
		Iterator it = parameterMap.entrySet().iterator();
		while (it.hasNext()) {
			Entry parmEntry = (Entry) it.next();
			BasicNameValuePair np = new BasicNameValuePair((String) parmEntry.getKey(), (String) parmEntry.getValue());
			param.add(np);
		}
		return param;
	}
}
